//
//  NSString+WNMiscellaneous.m
//  iOSOCProjectDirectory
//
//  Created by BHmac on 2017/6/12.
//  Copyright © 2017年 XieXuan. All rights reserved.
//

#import "NSString+WNMiscellaneous.h"

@implementation NSString (WNMiscellaneous)

#pragma mark ------------------------- 计算字符串大小

- (CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize
{
    NSDictionary *dict = @{NSFontAttributeName: font};
    CGSize textSize = [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
    return textSize;
}


#pragma mark ------------------------- 计算Html 格式文本大小

- (CGSize)htmlStrSizeWithFont:(UIFont *)font maxSize:(CGSize)masSize{
    
    NSMutableAttributedString * attributedString=  [[NSMutableAttributedString alloc] initWithData:[self dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
    [attributedString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, attributedString.length)];
    
    CGSize textSize = [attributedString boundingRectWithSize:masSize options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine context:nil].size;
    
    return textSize;
}


#pragma mark -------------------------- 去除HTML标签

+(NSString *)filterHTML:(NSString *)html
{
    NSScanner * scanner = [NSScanner scannerWithString:html];
    NSString * text = nil;
    while([scanner isAtEnd]==NO)
    {
        //找到标签的起始位置
        [scanner scanUpToString:@"<" intoString:nil];
        //找到标签的结束位置
        [scanner scanUpToString:@">" intoString:&text];
        //替换字符
        html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>",text] withString:@""];
    }
    
    return html;
}


#pragma mark －---------------------------- 去除空格

+(NSString *)nullStr:(NSString *)str;{
    if ([[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0 )
    {
        return @" ";
    }else{
        return  str;
    }
}


#pragma mark -------------------------------------------- 中文转拼音

+ (NSString *)transform:(NSString *)chinese
{
    NSMutableString *pinyin = [chinese mutableCopy];
    CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformMandarinLatin, NO);
    CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformStripCombiningMarks, NO);
    return [pinyin uppercaseString];//小写转成大写
}


#pragma mark -------------------------------------------- 判断空字符串

+ (NSString *)isBlankString:(NSString *)str{
    
    NSString *  nullStr = @"无";
    
    if (str == nil || str == NULL) {
        return nullStr;
    }
    else if ([str isKindOfClass:[NSNull class]]) {
        return nullStr;
    }
    else if ([[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        return nullStr;
    }
    else if([str isKindOfClass:[NSNumber class]]){
        
        return nullStr;
    }
    
    return str;
    
}


#pragma mark -------------------------------------------- 加密

+ (NSString *)encryption:(NSString *)string{
    
    NSString *securePassword = [WNEncryptionClass encryptWithContent:string type:kCCEncrypt key:SECUREKEY];
    return securePassword;
    
}


#pragma mark -------------------------------------------- 解密

+ (NSString *)decryption:(NSString *)string{
    
    NSString *newStr = [WNEncryptionClass encryptWithContent:string type:kCCDecrypt key:SECUREKEY];
    return newStr;
    
}

+ (NSString *)utf8toChinese:(NSString *)string{
    
    NSString * chineseStr =[string stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    return chineseStr;
    
}


#pragma mark -------------------------------------------- 设置 textField 提示文字的颜色

+ (NSAttributedString *)setTextFieldPlaceholderColor:(UITextField *)textField{
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[NSForegroundColorAttributeName] = WNTextGrayColor;
    NSAttributedString *attribute = [[NSAttributedString alloc] initWithString:textField.placeholder attributes:dict];
    return attribute;
    
}


#pragma mark -------------------------------------------- 自适应尺寸大小

+ (NSString *)autoWebAutoImageSize:(NSString *)html{
    
    NSString * regExpStr = @"<img\\s+.*?\\s+(style\\s*=\\s*.+?\")>";
    
    NSRegularExpression *regex=[NSRegularExpression regularExpressionWithPattern:regExpStr options:NSRegularExpressionCaseInsensitive error:nil];
    
    NSArray *matches=[regex matchesInString:html
                                    options:0
                                      range:NSMakeRange(0, [html length])];
    
    
    NSMutableArray * mutArray = [NSMutableArray array];
    for (NSTextCheckingResult *match in matches) {
        NSString* group1 = [html substringWithRange:[match rangeAtIndex:1]];
        [mutArray addObject: group1];
    }
    
    NSUInteger len = [mutArray count];
    for (int i = 0; i < len; ++ i) {
        html = [html stringByReplacingOccurrencesOfString:mutArray[i] withString: @"style=\"width:200px;height:100px;\""];
    }
    
    //    NSString * regExpStr = @"<img.*?>";
    //    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regExpStr options:NSRegularExpressionCaseInsensitive error:nil];
    //
    //    NSArray * matches = [regex matchesInString:html options:0 range:NSMakeRange(0, [html length])];
    //
    //    //match: 所有匹配到的字符,根据() 包含级
    //
    //    NSMutableArray *array = [NSMutableArray array];
    //
    //    for (NSTextCheckingResult *match in matches) {
    //
    //        for (int i = 0; i < [match numberOfRanges]; i++) {
    //            //以正则中的(),划分成不同的匹配部分
    //            NSString *component = [html substringWithRange:[match rangeAtIndex:i]];
    //
    //            [array addObject:component];
    //
    //        }
    //
    //    }
    //    DLog(@"%@",array);
    
    return html;
}


#pragma mark -------------------------------------------- 显示Html文本

-(NSAttributedString *)changToAttributedString{
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[self dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    return  attributedString;
}


@end
